import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/home'
import Layout from '../views/layout/index.vue'
import Cookies from 'js-cookie'

// 建立vue与路由的关系
Vue.use(VueRouter)

const routes = [
	{
		path: '/',
		name: 'index',
		title:"首页",
		meta:{isMenu:false,title:"首页"},
		component: Layout,
		redirect: "home",
		children: [{
			path: "/home",
			name: "Home",
			component: Home
		}]
	},
	{
		path: '/login',
		name: 'Login',
		meta:{isMenu:false},
		component: () => import( /* webpackChunkName: "about" */ '../views/login/index.vue')
	},
	{
		path: '/pms',
		title: "商品",
		name: 'pms',
		component:Layout,
		meta:{isMenu:true,title: "商品"},
		redirect: "/product/list",
		children: [{
				path: "/product/list",
				meta:{title: "商品列表"},
				title: "商品列表",
				component: () => import( /* webpackChunkName: "about" */ '../views/pms/product/index.vue')
			},
			{
				path: "/product/add",
				title: "添加商品",
				meta:{title: "添加商品"},
				component: () => import( /* webpackChunkName: "about" */ '../views/pms/product/add.vue')
			},
			{
				path: "/cate/list",
				title: "商品分类",
				meta:{title: "商品分类"},
				component: () => import( /* webpackChunkName: "about" */ '../views/pms/productCat/index.vue')
			},
			{
				path: "/type/list",
				title: "商品类型",
				meta:{title: "商品类型"},
				component: () => import( /* webpackChunkName: "about" */ '../views/pms/productType/index.vue')
			},
			{
				path: "/brand/list",
				title: "品牌管理",
				meta:{title: "品牌管理"},
				component: () => import( /* webpackChunkName: "about" */ '../views/pms/productBrand/index.vue')
			}
		]
	},
	{
		path: '*',
		name: 'Error',
		meta:{isMenu:false},
		component: () => import( /* webpackChunkName: "about" */ '../views/404.vue')
	}
]

const router = new VueRouter({
	routes
})

// 添加守卫
router.beforeEach((to, from, next) => {
	//查看访问路径是否是登录(/login)，如果是/login,直接放行
	if(to.path == "/login"){
		next();
		return;
	}
	
	//否则看是否登录了，如果登录了，直接放行，否则跳转到登录
	if(Cookies.get("loginToken") != null){
		next();
		return;
	}
	next("/login");
})

// 这里使用export default ruoter  导出当前文件里面的router对象
// 在另一个文件里面就可以使用import 随便一个名字 from 上面的文件（文件)
export default router